package com.pai4j.pgc.pojo.convert;
import com.google.common.collect.Lists;
import com.pai4j.common.enums.CommonStatusEnum;
import com.pai4j.common.enums.community.ResourcePathEnum;
import com.pai4j.common.util.JsonUtil;
import com.pai4j.domain.community.vo.response.resource.ResourceBaseResponseVO;
import com.pai4j.domain.community.vo.response.resource.ResourceDetailResponseVO;
import com.pai4j.domain.community.vo.response.resource.ResourceViewResponseVO;
import com.pai4j.pgc.entity.community.ResourceEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 程序员七七
 * @Date: 26.12.21 1:40 上午
 */
public class ResourceEntityConvert {


    public static ResourceDetailResponseVO convert(ResourceEntity resourceEntity) {
        if (resourceEntity == null) {
            return null;
        }
        ResourceDetailResponseVO resourceDetailResponseVO = new ResourceDetailResponseVO();
        BeanUtils.copyProperties(resourceEntity, resourceDetailResponseVO);
        resourceDetailResponseVO.setSubmitTime(resourceEntity.getCreateDate().getTime());
        resourceDetailResponseVO.setPrintSize(getPrintSize(resourceEntity.getSize()));
        resourceDetailResponseVO.setStatusDesc(CommonStatusEnum.getStatusDescByStatus(resourceEntity.getStatus()));
        resourceDetailResponseVO.setReviewStatusDesc(ReviewStatusEnum.getViewStatusDesc(resourceEntity.getReviewStatus()));
        if (StringUtils.isNotBlank(resourceEntity.getImages())) {
            resourceDetailResponseVO.setImageList(JsonUtil.fromJson(resourceEntity.getImages(), List.class));
        }
        if (resourceEntity.getReviewDate() != null) {
            resourceDetailResponseVO.setReviewTime(resourceEntity.getReviewDate().getTime());
        }
        resourceDetailResponseVO.setPrintPath(ResourcePathEnum.valueOf(resourceEntity.getPath()).getPath());
        resourceDetailResponseVO.setPrintPayType(ResourcePayTypeEnum.getPrintPayType(resourceEntity.getPayType()));
        resourceDetailResponseVO.setPrintPayAmount(getPrintPayAmount(resourceEntity.getPayType(), resourceEntity.getPayAmount()));
        return resourceDetailResponseVO;
    }

    public static List<ResourceDetailResponseVO> convertList(List<ResourceEntity> resourceEntities) {
        if (CollectionUtils.isEmpty(resourceEntities)) {
            return Lists.newArrayList();
        }
        return resourceEntities.stream().map(res -> convert(res)).collect(Collectors.toList());
    }

    /***
     * detailVo ->  baseVo
     * @param detailVo
     * @return
     */
    public static ResourceBaseResponseVO detail2Base(ResourceDetailResponseVO detailVo) {
        if (detailVo == null) {
            return null;
        }
        ResourceBaseResponseVO base = new ResourceBaseResponseVO();
        BeanUtils.copyProperties(detailVo, base);
        return base;
    }

    public static List<ResourceBaseResponseVO> detail2BaseList(List<ResourceEntity> resourceEntities) {
        if (CollectionUtils.isEmpty(resourceEntities)) {
            return Lists.newArrayList();
        }
        return resourceEntities.stream().map(res -> detail2Base(convert(res))).filter(x -> x != null).collect(Collectors.toList());
    }

    /***
     * detailVo ->  view
     * @param detailVo
     * @return
     */
    public static ResourceViewResponseVO detail2View(ResourceDetailResponseVO detailVo) {
        if (detailVo == null) {
            return null;
        }
        ResourceViewResponseVO view = new ResourceViewResponseVO();
        BeanUtils.copyProperties(detailVo, view);
        return view;
    }

    public static String getPrintPayAmount(Integer payType, Integer payAmount) {
        if (ResourcePayTypeEnum.FREE.getType().equals(payType)) {
            return "/";
        } else if (ResourcePayTypeEnum.COIN.getType().equals(payType)) {
            return payAmount + "猿气值";
        } else if (ResourcePayTypeEnum.MONEY.getType().equals(payType)) {
            return payAmount + "元";
        }
        return "/";
    }

    public static String getPrintSize(long size) {
        //如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
        if (size < 1024) {
            return String.valueOf(size) + "B";
        } else {
            size = size / 1024;
        }
        //如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
        //因为还没有到达要使用另一个单位的时候
        //接下去以此类推
        if (size < 1024) {
            return String.valueOf(size) + "KB";
        } else {
            size = size / 1024;
        }
        if (size < 1024) {
            //因为如果以MB为单位的话，要保留最后1位小数，
            //因此，把此数乘以100之后再取余
            size = size * 100;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "MB";
        } else {
            //否则如果要以GB为单位的，先除于1024再作同样的处理
            size = size * 100 / 1024;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "GB";
        }
    }
}
